package com.zm.rpc.protocol.enums;

import com.zm.rpc.protocol.request.RpcHeartBeat;
import com.zm.rpc.protocol.request.RpcRequest;
import com.zm.rpc.protocol.response.RpcResponse;
import com.zm.rpc.server.dispatcher.handler.EmptyHandler;
import com.zm.rpc.server.dispatcher.handler.IDispatcherHandler;
import com.zm.rpc.server.dispatcher.handler.PingHandler;
import com.zm.rpc.server.dispatcher.handler.RPCRequestHandler;

public enum RequestType {
    
    UNKNOW((byte)-1,EmptyHandler.class,Object.class),
    REQUEST((byte)0,RPCRequestHandler.class,RpcRequest.class),
    RESPONSE((byte)1,EmptyHandler.class,RpcResponse.class),
    HEARTBEAT((byte)2,PingHandler.class,RpcHeartBeat.class);


    private byte reqType;
    private Class<? extends  IDispatcherHandler<?>> requestHandler;
    private Class<?> requestContext;
    

    RequestType(byte type,Class<? extends IDispatcherHandler<?>> handler,Class<?> reqContext){
        reqType = type;
        requestHandler = handler;
        requestContext = reqContext;
    }

    public byte code(){
        return reqType;
    }

    public Class<? extends  IDispatcherHandler<?>> getHandlerClass(){
        return requestHandler;
    }

    public Class<?> getRequestContextClass(){
        return requestContext;
    }

    public static RequestType valueOf(byte code){
        RequestType type = UNKNOW;
        if(code == REQUEST.code()){
            type = REQUEST;
        }else if(code == RESPONSE.code()){
            type = RESPONSE;
        }else if(code == HEARTBEAT.code()){
            type = HEARTBEAT;
        }

        return type;
    }

}
